<template>
    <div>
        <el-select v-if="modelType === 1" v-model="scope.row.organizationName"
                   @visible-change="organizationVisibleChange" filterable
                   @change="updataSelectChange" placeholder="">
            <el-option
                v-for="(item, index) in organizationNewList"
                :key="index"
                :label="item.organizationName"
                :value="item.organizationId">
            </el-option>
        </el-select>
        <el-select v-if="modelType === 2" v-model="scope.row.departmentDesc"
                   @visible-change="departmentVisibleChange" filterable
                   @change="updataSelectChange" placeholder="">
            <el-option
                v-for="(item, index) in departmentNewList"
                :key="index"
                :label="item.description"
                :value="item.flexValueId">
            </el-option>
        </el-select>
        <el-select v-if="modelType === 3" v-model="scope.row.costCenterDescription"
                   @visible-change="costCenterVisibleChange" filterable
                   @change="updataSelectChange" placeholder="">
            <el-option
                v-for="(item, index) in costCenterNewList"
                :key="index"
                :label="item.description"
                :value="item.flexValueId">
            </el-option>
        </el-select>
    </div>
</template>
<script>
import { updateUserInfo } from "../../../components/axios/url";
import http from "../../../components/axios/fetch";

    export default {
        name: "cost-center-select",
        data(){
            return{
                organizationNewList:[],
                departmentNewList:[],
                costCenterNewList:[],
            }
        },
        props: {
            scope: {},
            modelType: "", // 控制是哪个下拉列表
            organizationList: {default:[]},
            departmentList: {default:[]},
            costCenterList: {default:[]},
        },
        mounted(){
        },
        methods: {
            organizationVisibleChange(value) { // 下拉接收 购买机构下拉数据
                if(value) this.organizationNewList = this.organizationList;
            },
            departmentVisibleChange(value) { // 下拉接收 购买部门下拉数据
                if(value) this.departmentNewList = this.departmentList;
            },
            costCenterVisibleChange(value) { // 下拉接收 成本中心下拉数据
                if(value) this.costCenterNewList = this.costCenterList;
            },
            updataSelectChange(event) {
                let param;
                if(this.modelType === 1) {  // 修改购买机构数据
                    param = { organizationId: event }
                }
                if(this.modelType === 2) {  // 修改购买部门数据
                    param = { department: event }
                }
                if(this.modelType === 3) {  // 修改成本中心数据
                    param = { costCenter: event }
                }
                try{
                    http.post(updateUserInfo, { ...param, userId: this.scope.row.userId }).then(res => {
                        if(res.data && res.data.resultMess === "success") {
                            this.$notify({
                                title: '提示',
                                message: '修改成功',
                                type: 'success'
                            });
                        }
                    })
                }catch(error){
                    this.$notify.error({
                        title: '提示',
                        message: '网络错误',
                    });
                }
            }

        }
    }
</script>

<style scoped>

</style>
